package controllers;

import static play.data.Form.form;
import play.*;
import play.mvc.*;
import java.util.Map;
import java.util.LinkedHashMap;
import views.html.*;
import play.data.*;
import static play.data.Form.*;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;
import model.Task;
import model.Users;
import model.Project;
import dataaccess.TaskDataAccess;
import dataaccess.UsersDataAccess;
import dataaccess.ProjectDataAccess;

@Security.Authenticated(SeguridadAdmin.class)
public class Application extends Controller {


      @Transactional
	public static Result saveUser() {
		Form<model.Users> form = form(model.Users.class).bindFromRequest();
		
			model.Users data = form.get();
			UsersDataAccess.save(data);
			return index();
		
	}
	
     @Transactional
        public static Result saveProject() {
                Form<model.Project> form = form(model.Project.class).bindFromRequest();
               
                        model.Project data = form.get();
                        ProjectDataAccess.save(data);
                        return index();
               
        }
     
    @Transactional
        public static Result saveTask() {
               DynamicForm form = form().bindFromRequest();

					String Title = form.get("Title");
					String Description = form.get("Description");
					long ProjectId = Long.parseLong(form.get("ProjectId"));
					long UserId = Long.parseLong(form.get("UserId"));
					
						Task t = new Task();
						
						t.setTitle(Title);
						t.setDescription(Description);
						t.setProjectId(ProjectId);
						t.setUserId(UserId);
						
						TaskDataAccess.save(t);
						
						return index();
                

        }
       
       
        @Transactional
        public static Result nuevaTarea() {
                return ok(nuevaTarea.render(form(model.Task.class)));
        }
   
    @Transactional
        public static Result nuevoProyecto() {
                return ok(nuevoProyecto.render(form(model.Project.class)));
        }
       
       
       @Transactional
	public static Result nuevoUsuario() {
		return ok(nuevoUsuario.render(form(model.Users.class)));
	}
	
    @Transactional
        public static Result usuarios() {
                return ok(usuarios.render("Usuarios",
                                UsersDataAccess.getAll()));
        }
       
       
    /**
     * handle de proyects
     */
     public static Map<String,String> projectOptions() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Project c: ProjectDataAccess.getAll()) {
            options.put(c.getProjectId().toString(), c.getProjectName());
        }
        return options;
    }
   
    /**
     * handle de Users
     */
        public static Map<String,String> usersOptions() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Users u: UsersDataAccess.getAll()) {
            options.put(u.getUserId().toString(), u.getUserName());
        }
        return options;
    }


   @Transactional
        public static Result index() {
                return ok(index.render(TaskDataAccess.getAll()));
        }
}

	

	
	
	
